package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.*;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.HealthReportService;
import com.itheima.utils.DateUtils;
import com.qiniu.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = HealthReportService.class)
@Transactional
public class HealthReportServiceImpl implements HealthReportService {

    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private FoodDao foodDao;
    @Autowired
    private MedicineDao medicineDao;
    @Autowired
    private TravelDao travelDao;
    @Autowired
    private AdviceDao adviceDao;


    public Result FindHealthReport(Integer id,String telephone) {
        try {
            //获取到保存在Redis中的json数据
            Jedis resource = jedisPool.getResource();
            String json = resource.get(telephone);
            //将json转换为对象
            JSONObject jsonObject = JSONObject.parseObject(json);
            Member member = JSON.toJavaObject(jsonObject,Member.class );
            resource.close();



            //根据预约id获取到获取到对应的预约数据
            Order order = orderDao.findByOrderId(id);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = order.getOrderDate();
            String orderDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
            Date newDate = new SimpleDateFormat("yyyy-MM-dd").parse(orderDate);
            order.setOrderDate(newDate);


            Integer setmealId = order.getSetmealId();
            //根据order中的setmealid获取相对的数据
            Setmeal setmeal = setmealDao.findById(setmealId);


            //根据预约的id获取到对应的report对象
            Report report = adviceDao.findById(order.getId());
            //将用户封装
            report.setMember(member);
            //将预约封装
            report.setOrder(order);
            //将套餐封装
            report.setSetmeal(setmeal);

            //根据reportid获取对应的运动，药物，膳食信息
            List<Travel> travels = travelDao.findTravelByReportId(report.getId());
            List<Food> foods = foodDao.findFoodByReportId(report.getId());
            List<Medicine> medicines = medicineDao.findMedicineByReportId(report.getId());

            report.setMedicines(medicines);
            report.setFoods(foods);
            report.setTravels(travels);


            return new Result(true,"获取数据成功",report);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取数据失败");
        }

    }
}
